extends HoldMagic


@onready var hit_area: Area2D = $HitArea
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var collision_shape_2d: CollisionShape2D = $HitArea/CollisionShape2D
var inited: bool = false


func use():
	if not inited:
		var shape = collision_shape_2d.shape as CircleShape2D
		shape.radius = item_data.item_config.attack_range
		inited = true
	
	var player = holder as Player
	animation_player.play("magic")
	
	var areas = hit_area.get_overlapping_areas()
	if areas.size() <= 0: return
	
	for area in areas:
		var source = area.source 
		shoot_gravity_magic(source)
		

func shoot_gravity_magic(attack_target: Node2D):
	var config = item_data.item_config as ItemBaseConfig
	var hit_target = attack_target
	var player = holder as Player
	
	var inst = (ResourceManager.get_resource("particle_gravity_attack_tip") as PackedScene).instantiate() as GravityAttackTip
	player.level.particles_parent.add_child(inst)
	inst.global_position = hit_target.ray_cast_point.global_position
	inst.sender = holder
	inst.attack = config.attack
	inst.critical_rate = config.critical_rate
	inst.critical_bonus = config.critical_bonus
	inst.knockback = config.knockback
	inst.affect_vertical_velocity = config.affect_vertical_velocity
	inst.cam_shake_duration = config.attack_cam_shake_duration
	inst.cam_shake_strength = config.attack_cam_shake_intensity
	inst.is_enemy = false
	
	AudioManager.play_sound("sfx_lightning_shoot")
